using B.S.RbacData.ErrorCode;
using ContentmanagementBaseWrite.API.Applications.Commands.Popularizations;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseWrite.API.Controllers
{
    /// <summary>
    /// 疾病科普管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PopularizationController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PopularizationController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public PopularizationController(IMediator mediator, ILogger<PopularizationController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增疾病科普
        /// </summary>
        /// <param name="command">新增疾病科普命令</param>
        /// <returns>新增结果</returns>
        [HttpPost("add")]
        public async Task<ApiResult<int>> AddPopularization([FromBody] AddNewPopularizationCommand command)
        {
            try
            {
                _logger.LogInformation("接收到新增疾病科普请求，标题：{Title}", command.PopularScienceTitle);

                var result = await _mediator.Send(command);

                _logger.LogInformation("疾病科普新增请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "疾病科普新增请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"疾病科普新增请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 更新疾病科普
        /// </summary>
        /// <param name="command">更新疾病科普命令</param>
        /// <returns>更新结果</returns>
        [HttpPut("update")]
        public async Task<ApiResult<int>> UpdatePopularization([FromBody] UpdatePopularizationCommand command)
        {
            try
            {
                _logger.LogInformation("接收到更新疾病科普请求，ID：{Id}，标题：{Title}", command.Id, command.PopularScienceTitle);

                var result = await _mediator.Send(command);

                _logger.LogInformation("疾病科普更新请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "疾病科普更新请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"疾病科普更新请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 批量删除疾病科普
        /// </summary>
        /// <param name="command">批量删除疾病科普命令</param>
        /// <returns>删除结果</returns>
        [HttpPost("batch-delete")]
        public async Task<ApiResult<int>> BatchDeletePopularization([FromBody] BatchDeletePopularizationCommand command)
        {
            try
            {
                _logger.LogInformation("接收到批量删除疾病科普请求，ID列表：{PopularizationIds}", string.Join(",", command.PopularizationIds));

                var result = await _mediator.Send(command);

                _logger.LogInformation("疾病科普批量删除请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "疾病科普批量删除请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"疾病科普批量删除请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }
    }
}